﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SecuritiesAnalysis.Common;
using SecuritiesAnalysis.MACalculator;
using SecuritiesAnalysis.CurrentData;
using System.IO;

using System.Windows.Forms.DataVisualization.Charting;


namespace SecuritiesAnalysis.ToolkitWinForm
{
    public partial class frmMAView : Form
    {
        private SecuritiesAnalysisDataContext ctx;
        private List<decimal> ma3;
        private List<decimal> ma5;
        private List<decimal> ma21;
        private List<DateTime> datelist;
        private List<T_Info_MA> MAlist;

        public frmMAView()
        {
            InitializeComponent();
            ctx = new SecuritiesAnalysisDataContext();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SecuritiesAnalysis.Interface.ICalculator ma = new MACalculator.Calculator();
            ma.Calculate();
            //ma.ReCalculate();
            //ma.Clear();


            //DataSpider ds = new DataSpider();
            //ds.UpdateCurrentPrice();
            MessageBox.Show("OK");


            //测试日志模块
            //try
            //{
            //    int i = 8;
            //    int j = 0;
            //    int c = i / j;
            //}
            //catch (Exception ex)
            //{
            //    Log.OutputError(ex.Message);
            //}

            //Series1
        }

        private void button2_Click(object sender, EventArgs e)
        {
            databindXY();
        }

        private void databindXY()
        {
            MAlist = ctx.T_Info_MAs.Where(
                o => o.StockCode == (int)(this.numericStockCode.Value)
                )
                .OrderByDescending(c=>c.Date)
                .Take((int)this.numericDays.Value)
                .ToList();

            this.datelist = new List<DateTime>();
            this.ma3 = new List<decimal>();
            this.ma5 = new List<decimal>();
            this.ma21 = new List<decimal>();

            foreach(T_Info_MA ma in MAlist)
            {
                datelist.Add(ma.Date);
                ma3.Add(ma.MA3.Value);
                ma5.Add(ma.MA5.Value);
                ma21.Add(ma.MA21.Value);
            }

            //string[] xValues = new string[7] { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
            //double[] yValues = new double[7] { 50, 70, 40, 30, 20, 80, 10 };

            //chart1.ChartAreas["ChartArea1"].AxisY.IsStartedFromZero = false;

            //chart1.Series[1].IsValueShownAsLabel = true;
            //chart1.Series[1].ChartType = SeriesChartType.Stock;
            //chart1.ChartAreas["ChartArea1"].AxisY.Interval = 0.01;

            chart1.Series["Series1"].Points.DataBindXY(datelist, ma3);
            chart1.Series["Series2"].Points.DataBindXY(datelist, ma5);
            chart1.Series["Series3"].Points.DataBindXY(datelist, ma21);
        }

        private void frmTest_Load(object sender, EventArgs e)
        {
            
        }

        private void chart1_CursorPositionChanging(object sender, CursorEventArgs e)
        {
            SetPosition(e.Axis, e.NewPosition);
        }

        private void chart1_CursorPositionChanged(object sender, CursorEventArgs e)
        {
            SetPosition(e.Axis, e.NewPosition);
        }

        // Set Cursor Position to display control.
        private void SetPosition(Axis axis, double position)
        {
            if (double.IsNaN(position))
                return;

            StringBuilder cursorinfo=new StringBuilder();

            if (axis.AxisName == AxisName.X)
            {
                // Convert Double to DateTime.
                DateTime dateTimeX = DateTime.FromOADate(position);
                cursorinfo.Append("日期：");
                cursorinfo.Append(dateTimeX.ToShortDateString());

                T_Info_MA currentma = this.MAlist.Where(o => o.Date == dateTimeX).FirstOrDefault();
                if (currentma == null)
                {
                    cursorinfo.Append("  休市");
                    chart1.Titles[0].Text = cursorinfo.ToString();
                }
                else
                {
                    cursorinfo.Append("  MA3：");
                    cursorinfo.Append(currentma.MA3.Value.ToString());
                    cursorinfo.Append("  MA5：");
                    cursorinfo.Append(currentma.MA5.Value.ToString());
                    cursorinfo.Append("  MA21：");
                    cursorinfo.Append(currentma.MA21.Value.ToString());

                    chart1.Titles[0].Text = cursorinfo.ToString();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // Series
            chart1.Series["Series3"].MarkerSize = 15;
            chart1.Series["Series3"].MarkerColor = Color.White;
            chart1.Series["Series3"].MarkerBorderColor = Color.Blue;
            chart1.Series["Series3"].MarkerBorderWidth = 2;
            
            //chart1.Series["Series3"].Points[20].

            //显示触发点的方法
            DateTime testdate = new DateTime(2008, 12, 30);
            chart1.Series["Series3"].Points.Where(p => p.XValue == testdate.ToOADate()).FirstOrDefault().MarkerStyle = (MarkerStyle)MarkerStyle.Parse(typeof(MarkerStyle), "Star5");
        }
    }
}
